package com.example.lab.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Admin;
import com.example.lab.entity.Room;
import com.example.lab.entity.MyReturn;
import com.example.lab.service.RoomService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Room)表控制层
 *
 * @author makejava
 * @since 2022-04-27 20:11:20
 */
@RestController
@RequestMapping("room")
public class RoomController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RoomService roomService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param room 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, Room room) {
        Page<Room> roomPage= new Page<>(page,limit);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        Page<Room> page1=this.roomService.page(roomPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.roomService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param room 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public  MyReturn insert(@RequestBody Room room) {
        boolean save= this.roomService.save(room);
        if(save){
            return  super.success(room,1L);
        }else {
            return super.failed(room,1L);
        }

    }
    /**
     * 修改数据
     *
     * @param room 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public  MyReturn update(@RequestBody Room room) {
        boolean update=this.roomService.updateById(room);
        if(update){
            return  super.success(room,1L);
        }else {
            return  super.failed(room,1L);
        }
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public  MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        boolean b=roomService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
    @RequestMapping("/getAllRoomNoPage")
    public MyReturn getAllDeptNoPage(){
        List<Room> list = roomService.list();
        return super.success(list, (long) list.size());
    }
}

